<?php
/*
Plugin Name: Add Post URL
Plugin URI: http://easwy.com/blog/wordpress/wp-posturl/
Description: This plugin allows you to insert a user specific text (such as copyright, credit, etc.) at the beginning/ending of all your posts.
Version: 1.1
Author: Easwy Yang
Author URI: http://easwy.com/
*/

/*  Copyright 2009-2010  Easwy Yang  (Homepage: http://easwy.com/)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


### Create Text Domain For Translations
add_action('init', 'posturl_textdomain');
function posturl_textdomain() {
    $plugin_dir = basename(dirname(__FILE__));
    load_plugin_textdomain( 'wp-posturl', 'wp-content/plugins/' . $plugin_dir, $plugin_dir );
}


### Function: Post URL Option Menu
add_action('admin_menu', 'posturl_menu');
function posturl_menu() {
    $plugin_dir = basename(dirname(__FILE__));
    if (function_exists('add_options_page')) {
        add_options_page(__('Add Post URL', 'wp-posturl'),
            __('Add Post URL', 'wp-posturl'), 'manage_options',
            $plugin_dir . '/posturl-options.php') ;
    }
}

### Function: Post URL: Add Post URL for Post
add_action('the_content', 'wp_posturl', 0);
function wp_posturl($text) {
    $posturl_options = get_option('posturl_options');

    // replace macros within the post, if enabled
    /*
    $iner_url_str = trim($posturl_options['iner_url_str']);
    if (!empty($iner_url_str))
    {
        $iner_url_str = str_replace("%site_url%", site_url('/'), $iner_url_str);
        $iner_url_str = str_replace("%site_name%", get_bloginfo('sitename'), $iner_url_str);
        $iner_url_str = str_replace("%post_url%", get_permalink(), $iner_url_str);
        $iner_url_str = str_replace("%post_title%", the_title('', '', false), $iner_url_str);
        $iner_url_str = stripslashes($iner_url_str);

        $text = str_replace("%wpurl%", $iner_url_str, $text);
    }
     */

    if ((is_single() && $posturl_options['add_to_single'])
        || (is_home() && $posturl_options['add_to_home'])
        || (is_page() && $posturl_options['add_to_page'])
        || (is_category() && $posturl_options['add_to_category'])
        || (is_tag() && $posturl_options['add_to_tag'])
        || (is_feed() && $posturl_options['add_to_feed'])
        || ((is_day() || is_month() || is_year()) && $posturl_options['add_to_archive'])
    ) {
        // Add header text if not empty
        $header_text = $posturl_options['header_text'];
        $header_text = trim($header_text);
        if (!empty($header_text))
        {
            $header_text = str_replace("%site_url%", site_url('/'), $header_text);
            $header_text = str_replace("%site_name%", get_bloginfo('sitename'), $header_text);
            $header_text = str_replace("%post_url%", get_permalink(), $header_text);
            $header_text = str_replace("%post_title%", the_title('', '', false), $header_text);
            $header_text = stripslashes($header_text);
            $text = $header_text . $text;
        }

        $footer_text = $posturl_options['footer_text'];
        $footer_text = trim($footer_text);
        if (!empty($footer_text))
        {
            $footer_text = str_replace("%site_url%", site_url('/'), $footer_text);
            $footer_text = str_replace("%site_name%", get_bloginfo('sitename'), $footer_text);
            $footer_text = str_replace("%post_url%", get_permalink(), $footer_text);
            $footer_text = str_replace("%post_title%", the_title('', '', false), $footer_text);
            $footer_text = stripslashes($footer_text);
            $text .= $footer_text;
        }

        // add credit?
        $add_credit = $posturl_options["add_credit"];
        //if ($add_credit) 
	if (false)
        {
            $text .= sprintf(__('<div style="margin-top: 0; margin-bottom: 15px; color: #888888; font-size: 80%%; font-style: italic"><p>Post Footer automatically generated by <a href="%1$s" style="color: #8888FF; text-decoration: underline;">wp-posturl plugin</a> for wordpress.</p></div>', 'wp-posturl'), 'http://easwy.com/blog/wordpress/wp-posturl/');
        }
    }

    return $text;
}

### Function: Post URL Options
add_action('activate_' . basename(dirname(__FILE__)) . '/wp-posturl.php', 'posturl_init');
function posturl_init() {
    posturl_textdomain();

    //$default_footer_text = addslashes(__('<div style="margin-top: 15px; font-style: italic"><p><strong>From</strong> <a href="%site_url%">%site_name%</a>, <strong>post</strong> <a href="%post_url%">%post_title%</a></p></div>','wp-posturl'));
    $default_footer_text = addslashes(__('<div style="margin-top: 15px; font-style: italic"><p><strong>来自：</strong> <a href="%site_url%">%site_name%</a>, <strong>转载请注明出处：</strong> <a href="%post_url%">%post_url%</a></p></div>','wp-posturl'));

    // if has posturl_options, upgrade it
    if ($posturl_options = get_option('posturl_options'))
    {
        // Upgrade add_credit
        if (isset($posturl_options['add_credit']))
        {
            if ($posturl_options['add_credit'] == 'Yes')
            {
                $posturl_options['add_credit'] = true;
            }
            else
            {
                $posturl_options['add_credit'] = false;
            }
        }
        else
        {
            $posturl_options['add_credit'] = true;
        }

        // Upgrade add_to_beginning
        if (isset($posturl_options['add_to_beginning'], $posturl_options['post_url_text']))
        {
            if ($posturl_options['add_to_beginning'] == 'No')
            {
                $posturl_options['footer_text'] = $posturl_options['post_url_text'];
            }
            else
            {
                $posturl_options['header_text'] = $posturl_options['post_url_text'];
            }

            // remove obsoleted options
            unset ($posturl_options['add_to_beginning']);
            unset ($posturl_options['post_url_text']);
        }
        else
        {
            $posturl_options['header_text'] = '';
            $posturl_options['footer_text'] = $default_footer_text;

            // remove obsoleted options
            unset ($posturl_options['add_to_beginning']);
            unset ($posturl_options['post_url_text']);
        }

        $posturl_options['add_to_home'] = false;
        $posturl_options['add_to_category'] = false;
        $posturl_options['add_to_tag'] = false;
        $posturl_options['add_to_archive'] = false;
        $posturl_options['add_to_single'] = true;
        $posturl_options['add_to_feed'] = false;
        $posturl_options['add_to_page'] = false;

        //$posturl_options['iner_url_str'] = '';

        // refresh options
        //update_option('posturl_options', $posturl_options);
        delete_option('posturl_options');
        add_option('posturl_options', $posturl_options);
    }
    // otherwise, install it
    else
    {
        // Add Options
        $posturl_options = array();
        $posturl_options['footer_text'] = $default_footer_text;
        $posturl_options['header_text'] = '';
        $posturl_options['add_credit'] = true;
        $posturl_options['add_to_beginning'] = false;
        $posturl_options['add_to_home'] = true;
        $posturl_options['add_to_category'] = true;
        $posturl_options['add_to_tag'] = true;
        $posturl_options['add_to_archive'] = true;
        $posturl_options['add_to_single'] = true;
        $posturl_options['add_to_feed'] = true;
        $posturl_options['add_to_page'] = true;
        //$posturl_options['iner_url_str'] = '';
        add_option('posturl_options', $posturl_options);
    }
}
?>
